{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sl = {\n",
    "    '1': '1.png',\n",
    "    '2': '2.png',\n",
    "    '3': '3.png',\n",
    "    '4': '4.png',\n",
    "    '5': '5.png',\n",
    "    '6': '6.png',\n",
    "    '7': '7.png',\n",
    "    '8': '8.png',\n",
    "    '9': '9.png',\n",
    "    '0': '0.png',\n",
    "    '-': 'minus.png',\n",
    "    '+': 'plus.png',\n",
    "    '*': 'umn.png',\n",
    "    '/': 'del.png',\n",
    "      }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def mod_img(name, i):\n",
    "    img = cv2.imread(f'dataset/{name}.png')\n",
    "    img = cv2.inRange(img, (250, 250, 250), (255, 255, 255))\n",
    "    h, w = img.shape\n",
    "    if h > w:\n",
    "        w = round((w / h) * 30)\n",
    "        h = 30\n",
    "    else:\n",
    "        h = round((h / w) * 30)\n",
    "        w = 30\n",
    "\n",
    "    img = cv2.resize(img, (w, h))\n",
    "    dw = round((36 - w) / 2)\n",
    "    dh = round((36 - h) / 2)\n",
    "    img = cv2.copyMakeBorder(img, dh, dh , dw, dw, cv2.BORDER_CONSTANT, 0)\n",
    "    cv2.imwrite(f'dataset/{i}/img.png', img)\n",
    "    return img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "36 36\n",
      "36 35\n",
      "36 36\n",
      "36 36\n",
      "36 36\n",
      "36 35\n",
      "36 35\n",
      "36 35\n",
      "36 36\n",
      "36 37\n",
      "36 36\n",
      "36 37\n",
      "36 35\n",
      "36 36\n"
     ]
    }
   ],
   "source": [
    "imgs_files = [str(i) for i in range(10)] + ['plus', 'minus', 'umn', 'del']\n",
    "for i, imgname in enumerate(imgs_files):\n",
    "    img = mod_img(imgname, i)\n",
    "    h, w = img.shape\n",
    "    print(h, w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "cv2.imshow('img', img)\n",
    "while True:\n",
    "    \n",
    "    k = cv2.waitKey(1)\n",
    "    if k == 27:\n",
    "        break\n",
    "cv2.destroyAllWindows()\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = Image.open('dataset/0.png')\n",
    "i = i.resize((20, 20), Image.BOX)\n",
    "i.save('test.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "79622e2cdd399c04171d1ae422da3eb99546339dc74b7429cf74d873e1d44f60"
  },
  "kernelspec": {
   "display_name": "Python 3.8.2 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
